<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Spinatiev Ivan
 * Date: 9/27/12
 * Time: 2:53 PM
 * To change this template use File | Settings | File Templates.
 */


class Db_CleanUpDataBase extends Engine {

	/**
	 * @static
	 * @return mixed
	 * This function get all tables in base "Engine" where is field delete
	 */
	public static function getTablesWithFieldDeleted() {
		$dbName = conf('DB_NAME');
		return self::$DB->select("SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE 'deleted' AND TABLE_SCHEMA = '{$dbName}';");
	}

	/**
	 * @static
	 * @param $tablesNameArray
	 * @return bool
	 * This function delete all rows in table where field delete = 1
	 */
	public static function deleteDeletedRows($tablesNameArray) {
		$number = 0;
		if ($tablesNameArray) {
			foreach ($tablesNameArray as $tableName) {
				self::$DB->q("DELETE FROM ?f WHERE `deleted` = 1;", $tableName);
				if (self::$DB->affectedRows() > 0) {
					$number = self::$DB->mysqlModifiedRows();
				}
				$numberDeleted[$tableName] = $number;
				$number = 0;
			}
			return $numberDeleted;
		} else {
			return false;
		}
		return true;
	}
}

